const { salesMenModel } = require("./models/salesMenModel");
//获取销售员数据
module.exports.getSalesMen = async ({
  type = "",
  value = "",
  pageSize,
  currentPage,
}) => {
  // 获取总数据条数：
  const total = await salesMenModel.countDocuments();
  //计算总页数
  const pages = Math.ceil(total / pageSize);
  //
  const salesMen = await salesMenModel
    .find({
      delete: false,
      $or: [
        { name: { $regex: value, $options: "$i" } },
        { age: { $regex: value, $options: "$i" } },
        { gender: { $regex: value, $options: "$i" } },
      ],
    })
    .limit(parseInt(pageSize)) //限制数据 每页显示最大数据
    .skip((currentPage - 1) * pageSize); //跳过数据 第一页没有
  return {
    total,
    pages,
    salesMen,
  };
};

//新增销售员
module.exports.addSalesMen = async (params) => {
  return await salesMenModel.create(params);
};
//删除销售员
module.exports.delSalesMen = async (params) => {
  return salesMenModel.updateOne(params, { delete: true });
};
//获取单个销售老哥
module.exports.getSalesMenById = async (params) => {
  return salesMenModel.find(params);
};
//修改销售员
module.exports.updateSalesMen = async (params) => {
  return salesMenModel.updateOne({ _id: params._id }, params);
};
